from tkinter import *
from tkinter.messagebox import *

class Calculator(Tk):
    def __init__(self):
        super().__init__()
        self.geometry("360x450+800+400")
        self.title("计算器")
        # self.resizable(0, 0)
        self.symbol=["+","-","*","/","."]
        self.result = "0"
        self.temp = "0"
        self.result_temp = ""
        self.num_or_symbol = True
        self.setup_UI()

    def setup_UI(self):
        # 添加显示结果的标签
        self.Var_result_old = StringVar()
        self.Label_result_old = Label(self,textvariable=self.Var_result_old,width=40,height=8,bg="lightgray",justify="left",anchor="se")
        self.Label_result_old.grid(row=0,column=0,columnspan=4)

        self.Var_result_now = StringVar()
        self.Label_result_now = Label(self,textvariable=self.Var_result_now,width=40,height=2,bg="gray",fg="white",justify="left",anchor="se")
        self.Label_result_now.grid(row=1,column=0,columnspan=4)
        # 添加运算符号按钮
        self.Button_time = Button(self,text="*",width=5,font=("微软雅黑",16,"bold"),command=self.get_time)
        self.Button_time.grid(row=2,column=0)

        self.Button_min = Button(self,text="-",width=5,font=("微软雅黑",16,"bold"),command=self.get_min)
        self.Button_min.grid(row=2,column=1)

        self.Button_plus = Button(self,text="+",width=5,font=("微软雅黑",16,"bold"),command=self.get_plus)
        self.Button_plus.grid(row=2,column=2)

        self.Button_div = Button(self,text="/",width=5,font=("微软雅黑",16,"bold"),command=self.get_div)
        self.Button_div.grid(row=2,column=3)
        # 添加数字按钮
        self.Button_7 = Button(self,text="7",width=5,font=("微软雅黑",16,"bold"),command=self.get_7)
        self.Button_7.grid(row=3,column=0)

        self.Button_8 = Button(self,text="8",width=5,font=("微软雅黑",16,"bold"),command=self.get_8)
        self.Button_8.grid(row=3,column=1)

        self.Button_9 = Button(self,text="9",width=5,font=("微软雅黑",16,"bold"),command=self.get_9)
        self.Button_9.grid(row=3,column=2)

        self.Button_4 = Button(self,text="4",width=5,font=("微软雅黑",16,"bold"),command=self.get_4)
        self.Button_4.grid(row=4,column=0)

        self.Button_5 = Button(self,text="5",width=5,font=("微软雅黑",16,"bold"),command=self.get_5)
        self.Button_5.grid(row=4,column=1)

        self.Button_6 = Button(self,text="6",width=5,font=("微软雅黑",16,"bold"),command=self.get_6)
        self.Button_6.grid(row=4,column=2)

        self.Button_1 = Button(self,text="1",width=5,font=("微软雅黑",16,"bold"),command=self.get_1)
        self.Button_1.grid(row=5,column=0)

        self.Button_2 = Button(self,text="2",width=5,font=("微软雅黑",16,"bold"),command=self.get_2)
        self.Button_2.grid(row=5,column=1)

        self.Button_3 = Button(self,text="3",width=5,font=("微软雅黑",16,"bold"),command=self.get_3)
        self.Button_3.grid(row=5,column=2)
        
        self.Button_percent = Button(self,text="%",width=5,font=("微软雅黑",16,"bold"),command=self.get_percent)
        self.Button_percent.grid(row=6,column=0)
        
        self.Button_0 = Button(self,text="0",width=5,font=("微软雅黑",16,"bold"),command=self.get_0)
        self.Button_0.grid(row=6,column=1)

        self.Button_point = Button(self,text=".",width=5,font=("微软雅黑",16,"bold"),command=self.get_point)
        self.Button_point.grid(row=6,column=2)

        self.Button_AC = Button(self,text="AC",width=5,font=("微软雅黑",16,"bold"),command=self.ac_button)
        self.Button_AC.grid(row=3,column=3)

        self.Button_back = Button(self,text="<-",width=5,font=("微软雅黑",16,"bold"),command=self.get_back)
        self.Button_back.grid(row=4,column=3)

        self.Button_equ = Button(self,text="=",width=5,height=3,font=("微软雅黑",16,"bold"),command=self.get_equ)
        self.Button_equ.grid(rowspan=2,row=5,column=3)



    def ac_button(self):
        self.Var_result_now.set("0")
        self.Var_result_old.set("0")
        self.result = "0"
        self.temp = "0"
        self.result_temp = ""
        self.num_or_symbol = True

    def get_1(self):
        if self.num_or_symbol == False:
            self.temp = "0"
            self.num_or_symbol = True

        if self.temp == "0":
            self.temp = "1"
        else:
            self.temp += "1"
        self.cal_notequ_num()
 
    def get_2(self):
        if self.num_or_symbol == False:
            self.temp = "0"
            self.num_or_symbol = True

        if self.temp == "0":
            self.temp = "2"
        else:
            self.temp += "2"
        self.cal_notequ_num()
 
    def get_3(self):
        if self.num_or_symbol == False:
            self.temp = "0"
            self.num_or_symbol = True

        if self.temp == "0":
            self.temp = "3"
        else:
            self.temp += "3"
        self.cal_notequ_num()
 
    def get_4(self):
        if self.num_or_symbol == False:
            self.temp = "0"
            self.num_or_symbol = True

        if self.temp == "0":
            self.temp = "4"
        else:
            self.temp += "4"
        self.cal_notequ_num()
 
    def get_5(self):
        if self.num_or_symbol == False:
            self.temp = "0"
            self.num_or_symbol = True

        if self.temp == "0":
            self.temp = "5"
        else:
            self.temp += "5"
        self.cal_notequ_num()
 
    def get_6(self):
        if self.num_or_symbol == False:
            self.temp = "0"
            self.num_or_symbol = True

        if self.temp == "0":
            self.temp = "6"
        else:
            self.temp += "6"
        self.cal_notequ_num()
 
    def get_7(self):
        if self.num_or_symbol == False:
            self.temp = "0"
            self.num_or_symbol = True

        if self.temp == "0":
            self.temp = "7"
        else:
            self.temp += "7"
        self.cal_notequ_num()
 
    def get_8(self):
        if self.num_or_symbol == False:
            self.temp = "0"
            self.num_or_symbol = True

        if self.temp == "0":
            self.temp = "8"
        else:
            self.temp += "8"
        self.cal_notequ_num()
 
    def get_9(self):
        if self.num_or_symbol == False:
            self.temp = "0"
            self.num_or_symbol = True

        if self.temp == "0":
            self.temp = "9"
        else:
            self.temp += "9"
        self.cal_notequ_num()
 
    def get_0(self):
        if self.num_or_symbol == False:
            self.temp = "0"
            self.num_or_symbol = True

        if self.temp == "0":
            self.temp = "0"
        else:
            self.temp += "0"
        self.cal_notequ_num()
    
    def get_point(self):
        if self.num_or_symbol == False:
            self.temp = "0"
            self.num_or_symbol = True

        if self.temp == "0":
            self.temp = "."
        else:
            self.temp += "."
        self.cal_notequ_num()

    def get_plus(self):
        if self.num_or_symbol == False:
            self.temp = self.temp[:-1] + "+"
            self.result_temp = self.temp
        else:
            if self.temp == "0":
                self.temp = "+"
            else:
                self.result = self.result_temp + self.temp
                self.temp += "+"
                self.result_temp += self.temp
                self.cal_notequ_symbol()


    def get_min(self):
        if self.num_or_symbol == False:
            self.temp = self.temp[:-1] + "-"
            self.result_temp = self.temp
        else:
            if self.temp == "0":
                self.temp = "-"
            else:
                self.result = self.result_temp + self.temp
                self.temp += "-"
                self.result_temp += self.temp
                self.cal_notequ_symbol()
  
    def get_time(self):
        if self.num_or_symbol == False:
            self.temp = self.temp[:-1] + "*"
            self.result_temp = self.temp
        else:
            if self.temp == "0":
                self.temp = "*"
            else:
                self.result = self.result_temp + self.temp
                self.temp += "*"
                self.result_temp += self.temp
                self.cal_notequ_symbol()

    def get_div(self):
        if self.num_or_symbol == False:
            self.temp = self.temp[:-1] + "/"
            self.result_temp = self.temp
        else:
            if self.temp == "0":
                self.temp = "/"
            else:
                self.result = self.result_temp + self.temp
                self.temp += "/"
                self.result_temp += self.temp
                self.cal_notequ_symbol()

    def get_percent(self):
        if self.num_or_symbol == False:
            self.temp = self.temp[:-1] + "%"
            self.result_temp = self.temp
        else:
            if self.temp == "0":
                self.temp = "%"
            else:
                self.result = self.result_temp + self.temp
                self.temp += "%"
                self.result_temp += self.temp
                self.cal_notequ_symbol()


    def get_back(self):
         # 获得原有内容
        content = str(eval(self.result))
        if content == "0":
            return
        else:
            content = content[:-1]
            self.Var_result_now.set(content)
            self.temp = content
        self.result_temp = ""
        self.result = "0"

    def cal_notequ_num(self):
        self.Var_result_now.set(self.temp)


    def cal_notequ_symbol(self):
        self.result = eval(self.result)
        self.Var_result_old.set(self.result_temp)
        self.Var_result_now.set(self.result)
        self.num_or_symbol = False

    def get_equ(self):
        if self.temp[-1] in self.symbol:
            result = eval(self.temp[:-1])
            self.Var_result_now.set(result)
        else:
            self.result = self.result_temp + self.temp
            self.Var_result_now.set(eval(self.result))
            self.Var_result_old.set("")
            self.temp = str(eval(self.result))
            self.result_temp = ""







    def show(self):
        self.mainloop()

if __name__ == "__main__":
    this_gui = Calculator()
    this_gui.show()
